<template>
  <div class="goods-wrap">
    <div class="goods-list left">
      <div class="goods-item" v-for="item in leftGoods" :key="item.id" @click="toDetail(item.id)">
        <img :src="item.image" alt="">
        <p class="goods-name">{{item.name}}</p>
        <p class="goods-desc">{{ item.description }}</p>
        <span class="discount">会员价</span>
        <div class="price">
          <span class="current-price">￥{{ item.price }}</span>
          <del>￥{{ item.old_price }}</del>
          <!-- 加购 -->
          <div class="add-cart" @click.stop="add(item)">+</div>
        </div>
      </div>
    </div>
    <div class="goods-list">
      <div class="goods-item" v-for="item in RightGoods" :key="item.id" @click="toDetail(item.id)">
        <img :src="item.image" alt="">
        <p class="goods-name">{{item.name}}</p>
        <p class="goods-desc">{{ item.description }}</p>
        <span class="discount">会员价</span>
        <div class="price">
          <span class="current-price">￥{{ item.price }}</span>
          <del>￥{{ item.old_price }}</del>
          <!-- 加购 -->
          <div class="add-cart" @click.stop="add(item)">+</div>
        </div>
      </div>
    </div>
    <van-action-sheet v-model:show="isShow" closeable>
            <div class="pop" v-if="goods">
                <div class="header">
                    <van-image :src="goods.image" />
                    <div class="detail">
                        <span class="title">{{ goods.name }}</span>
                        <span class="price">￥<span class="rmb">{{ goods.price }}</span></span>
                    </div>
                </div>
                <div class="content">
                    <span>购买数量</span>
                    <van-field name="stepper">
                        <template #input>
                            <van-stepper v-model="num" theme="round" button-size="32" />
                        </template>
                    </van-field>
                </div>
                <div class="foot">
                    <van-button type="primary" @click="addCart(goods.id)">确定</van-button>
                </div>
            </div>
        </van-action-sheet>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import axios from '../utils/axios'
const props = defineProps({
  goodsList: {
    type: Array,
    default: []
  }
})
// 获取路由实例
const router = useRouter()
// 获取vuex仓库实例
const store = useStore()
// 将商品按id基偶分组
const leftGoods = computed(() => {
  return props.goodsList.filter(item => item.id % 2 === 0)
})
const RightGoods = computed(() => {
  return props.goodsList.filter(item => item.id % 2 !== 0)
})
// 购买弹窗
const isShow = ref(false)
// 购买数量
const num = ref(1)
const goods = ref('')
const add = (item)=>{
  num.value = 1;
  isShow.value = true
  goods.value = item
}

// 子组件事件传递
const emit = defineEmits(['child-click'])

// 添加商品到购物车
const addCart = (dishid) => {
  // console.log(dishid)
  isShow.value = false
  axios.post('/add_shop', { dishid: dishid, number: num.value}).then((res) => {
    // console.log(res)
    emit('child-click')
    store.dispatch('fetchCartCount')
  })
}
// 跳转商品详情页
const toDetail = (goods_id) => {
  router.push(`/goods/${goods_id}`);
}
</script>

<style lang="less" scoped>
.goods-wrap{
  display: flex;
  .goods-list{
    width: 50%;
    &.left{
      margin-right: 5px;
    }
    .goods-item{
      box-sizing: border-box;
      margin-bottom: 8px;
      padding: 5px;
      color: #9fa19f;
      background-color: #fff;
      border: 1px solid transparent;
      border-radius: 5px;
      img{
        width: 100%;
        object-fit: contain;
      }
      .goods-name, .goods-desc, .discount, .price{
        margin-top: 6px;
      }
      .goods-name{
        color: #303133;
        font-size: 16px;
      }
      .goods-desc{
        // 放置文本换行
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .discount{
        display: inline-block;
        color: #fb4e44;
        padding: 3px;
        border-radius: 5px;
        border: 1px solid #fb4e44;
      }
      .price{
        position: relative;
        .current-price{
          font-size: 16px;
          font-weight: bold;
          margin-right: 5px;
          color: #fb4e44;
        }
        .add-cart{
          position: absolute;
          width: 24px;
          height: 24px;
          border-radius: 50%;
          background-color: #00a73c;
          color: #fff;
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 20px;
          bottom: 0;
          right: 5px;
        }
      }
    }
  }
}
</style>